import Vue from 'vue'
import co from 'co'
import * as Util from '../../components/Util'

let disableGen = function * (data) {
  let owe = yield Vue.resetpost('rs/sql/getOverdue', {data: {f_user_id: data.f_user_id}}, {resolveMsg: null, rejectMsg: null})
  console.log('表具停用看是否有欠费', owe.data)
  let hasHandplan = yield Vue.resetpost('rs/sql/singleTable', {data: {tablename: 't_handplan', condition: `f_meter_state = '未抄表' and f_user_id = '${data.f_user_id}'`}}, {resolveMsg: null, rejectMsg: null})
  console.log('是否有未抄表数据', hasHandplan.data)
  if (owe.data.length > 0 && hasHandplan.data.length > 0) {
    Vue.showAlert('该表具还有欠费或者未抄表数据，不能停用！！', 'danger', 0)
  } else {
    Vue.resetpost('rs/entity/t_disable', data, {resolveMsg: '表具停用成功', rejectMsg: '表具停用失败'})
  }
}

let LogicService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.LogicService = Vue.prototype.$LogicService = LogicService
  },

  // 表具启用
  meterEnable (model, row) {
    let data = {
      userfile: {
        f_user_id: row.f_user_id,
        f_table_state: '正常',
        version: row.version
      },
      record_userinfo: row.f_userinfo_id,
      f_user_id: row.f_user_id,
      f_userinfo_id: row.f_userinfo_id,
      f_user_name: row.f_user_name,
      f_enable_state: '有效',
      f_reason: model.f_reason[0],
      f_othereason: model.f_othereason,
      f_operat_type: '启用',
      f_describe: `${Util.f.name}对用户${row.f_user_name}进行表具启用操作`,
      f_state: '有效',
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    return Vue.resetpost('rs/entity/t_enable', data, {resolveMsg: '表具启用成功', rejectMsg: '表具启用失败'})
  },

  // 表具停用
  meterDisable (model, row) {
    let data = {
      userfile: {
        f_user_id: row.f_user_id,
        f_table_state: '停用',
        version: row.version
      },
      record_userinfo: row.f_userinfo_id,
      f_user_id: row.f_user_id,
      f_userinfo_id: row.f_userinfo_id,
      f_user_name: row.f_user_name,
      f_disable_state: '有效',
      f_reason: model.f_reason[0],
      f_othereason: model.f_othereason,
      f_operat_type: '停用',
      f_describe: `${Util.f.name}对用户${row.f_user_name}进行表具停用操作`,
      f_state: '有效',
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    let disable = disableGen(data)
    return co(disable)
  },

  // 其他收费
  otherCharge (model, row) {
    let data = {
      record_userinfo: row.f_userinfo_id,
      f_user_id: row.f_user_id,
      f_number: model.f_number,
      f_unitprice: model.f_unitprice,
      f_collection: model.f_collection,
      f_note: model.f_note,
      f_charge_type: model.f_charge_type[0],
      f_brand_spec: model.f_brand_spec[0],
      f_userinfo_id: row.f_userinfo_id,
      f_operat_type: '其他收费',
      f_describe: `${Util.f.name}对用户${row.f_user_name}进行其他收费操作`,
      f_state: '有效',
      f_charge_state: '有效',
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname
    }
    return Vue.resetpost('rs/entity/t_othercharge', data, {resolveMsg: '其他收费成功', rejectMsg: '其他收费失败'})
  },

  // 过户管理
  changeAccount (model) {
    // console.log('过户。，。。', model)
    return Vue.resetpost('rs/logic/transferSave', {data: {model: model}}, {resolveMsg: '过户成功', rejectMsg: '过户失败'})
  },

  // 批量变更抄表员
  updateInputtor (data) {
    return Vue.resetpost('rs/logic/updateBatch', data, {resolveMsg: '批量修改成功', rejectMsg: '批量抄表员变更失败'})
  },
  // 批量变更气价
  updateGasPrice (data) {
    return Vue.resetpost('rs/logic/updateBatchPrice', data, {resolveMsg: '批量修改成功', rejectMsg: '批量变更气价失败'})
  },
  // 增加气表品牌
  addGasBrand (model, Arrlist) {
    let data = {
      f_meter_brand: model.f_meter_brand,
      f_manufacturers: model.f_manufacturers,
      f_meter_type: model.f_meter_type,
      f_police_gas: model.f_police_gas ? model.f_police_gas : 0,
      f_card_password: model.f_card_password,
      f_topup_ceil: model.f_topup_ceil ? model.f_topup_ceil : 0,
      f_overdr_lines: model.f_overdr_lines ? model.f_overdr_lines : 0,
      f_meter_limit: model.f_meter_limit ? model.f_meter_limit : 0,
      f_checkout_limit: model.f_checkout_limit ? model.f_checkout_limit : 0,
      f_alias: model.f_alias,
      gasmodellist: Arrlist
    }
    return Vue.resetpost('rs/logic/addGasBrand', {data: data}, {resolveMsg: '添加气表成功', rejectMsg: '增加气表失败'})
  },
  // 修改气表品牌
  modifyGasBrand (model, Arrlist) {
    console.log('气表品牌保存，，', model)
    return Vue.resetpost('rs/logic/updataGasBrand', {data: {data: model, gasmodellist: Arrlist}}, {resolveMsg: '修改气表品牌成功', rejectMsg: '修改气表品牌失败'})
  },
  // 删除气表品牌
  delGasBrand (f_id) {
    return Vue.resetpost('rs/logic/delGasBrand', {data: {id: f_id}}, {resolveMsg: '删除成功', rejectMsg: '删除失败'})
  },
  createMessage (data) {
    return Vue.resetpost('rs/logic/messageBatchOperate', data, {resolveMsg: '生成短信成功', rejectMsg: '生成短信失败'})
  },
  gasPrice (model, row) {
    console.log('model', model)
    let data = {
      f_price_type: model.f_price_type[0],
      f_price_name: model.f_price_name,
      f_user_type: model.f_user_type[0],
      f_gasproperties: model.f_gasproperties[0],
      f_stairmonths: model.f_stairmonths,
      f_perform_date: model.f_perform_date,
      f_end_date: '',
      f_state: '有效',
      detailprice: model.detailprice,
      type: '添加'
    }
    if (data.f_price_type === '固定气价') {
      if (data.detailprice[0].id) {
        delete data.detailprice[0].id
      }
      data.detailprice[0].f_price_name = 1
    } else if (data.f_price_type === '混合气价') {
      if (delete data.detailprice[0].id) {
        delete data.detailprice[0].id
      }
      data.detailprice[0].f_price_name = 1
      data.detailprice[0].f_ratio = model.detailprice[0].f_ratio / 100
      if (delete data.detailprice[1].id) {
        delete data.detailprice[1].id
      }
      data.detailprice[1].f_price_name = 2
      data.detailprice[1].f_ratio = 1 - data.detailprice[0].f_ratio
    } else if (data.f_price_type === '阶梯气价') {
      data.detailprice.forEach((item, index) => {
        data.detailprice[index].f_price_name = index + 1
        if (data.detailprice[index].id) {
          if (delete data.detailprice[index].id) {
            delete data.detailprice[index].id
          }
        } else if (data.detailprice[index].f_ratio) {
          data.detailprice[index].f_ratio = null
        }
      })
    }
    if (row && row.id != null) {
      data.oldprice = {
        id: row.id,
        f_state: '无效',
        f_end_date: model.f_perform_date
      }
      data.type = '修改'
      data.f_price_id = row.f_price_id
    }
    return Vue.resetpost('rs/logic/gasprice', {data: data})
  },
  sendMessage (condition) {
    return Vue.resetpost('rs/logic/sendSms', {condition: condition})
  },
  deletMessage (condition) {
    return Vue.resetpost('rs/logic/deleteSms', {condition: condition}, {resolveMsg: '删除短信成功', rejectMsg: '删除短信失败'})
  }
}

export default LogicService
